using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using ComponentFactory.Krypton.Toolkit;
using System.ComponentModel;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Drawing;


namespace revoStore
{
    [System.Drawing.ToolboxBitmapAttribute(typeof(System.Windows.Forms.ListView))]
    public partial class MyListView : ListView
    {
        private IPalette _palette;

        private PaletteRedirect _paletteRedirect;
        private Boolean _enableHeaderRendering = false;
        [Browsable(true), Category("Appearance-Extended")]
        [DefaultValue("False")]
        public Boolean EnableHeaderRendering
        {
            get { return _enableHeaderRendering; }
            set
            {
                _enableHeaderRendering = value;
                Invalidate();
            }
        }

        private Boolean _enableHeaderHotTrack = false;
        [Browsable(true), Category("Appearance-Extended")]
        [DefaultValue("False")]
        public Boolean EnableHeaderHotTrack
        {
            get { return _enableHeaderHotTrack; }
            set
            {
                _enableHeaderHotTrack = value;
                Invalidate();
            }
        }

        private Boolean _enableHeaderGlow = false;
        [Browsable(true), Category("Appearance-Extended")]
        [DefaultValue("False")]
        public Boolean EnableHeaderGlow
        {
            get { return _enableHeaderGlow; }
            set
            {
                _enableHeaderGlow = value;
                Invalidate();
            }
        }

        private Boolean _persistentColors = false;
        [Browsable(true), Category("Appearance-Extended")]
        [DefaultValue("False")]
        public Boolean PersistentColors
        {
            get { return _persistentColors; }
            set
            {
                _persistentColors = value;
                Invalidate();
            }
        }

        private Boolean _useStyledColors = false;
        [Browsable(true), Category("Appearance-Extended")]
        [DefaultValue("False")]
        public Boolean UseStyledColors
        {
            get { return _useStyledColors; }
            set
            {
                _useStyledColors = value;
                Invalidate();
            }
        }

        private Color _gradientStartColor = Color.White;
        [Browsable(true), Category("Appearance-Extended")]
        [DefaultValue("Color.White")]
        public Color GradientStartColor
        {
            get { return _gradientStartColor; }
            set
            {
                _gradientStartColor = value;
                Invalidate();
            }
        }

        private Color _gradientEndColor = Color.Gray;
        [Browsable(true), Category("Appearance-Extended")]
        [DefaultValue("Color.Gray")]
        public Color GradientEndColor
        {
            get { return _gradientEndColor; }
            set
            {
                _gradientEndColor = value;
                Invalidate();
            }
        }

        private Color _gradientMiddleColor = Color.LightGray;
        [Browsable(true), Category("Appearance-Extended")]
        [DefaultValue("Color.Gray")]
        public Color GradientMiddleColor
        {
            get { return _gradientMiddleColor; }
            set
            {
                _gradientMiddleColor = value;
                Invalidate();
            }
        }


        public MyListView()
        {
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            UpdateStyles();
            this.OwnerDraw = true;
            if (_palette != null)
            {
                _palette.PalettePaint += OnPalettePaint;
                //_palette.PalettePaint += New EventHandler(Of PaletteLayoutEventArgs)(AddressOf OnPalettePaint)
            }
            KryptonManager.GlobalPaletteChanged += OnGlobalPaletteChanged;
            //KryptonManager.GlobalPaletteChanged += New EventHandler(AddressOf OnGlobalPaletteChanged)
            _palette = KryptonManager.CurrentGlobalPalette;
            _paletteRedirect = new PaletteRedirect(_palette);
            //InitializeComponent();
        }

        protected override void OnDrawColumnHeader(DrawListViewColumnHeaderEventArgs e)
        {
            //base.OnDrawColumnHeader(e);            
            if (_enableHeaderRendering == true)
            {
                Rectangle rect = e.Bounds;
                rect.Height = rect.Height - 2;
                rect.Width = rect.Width - 0;
                Graphics g = e.Graphics;
                Point mouse = new Point();
                mouse = PointToClient(Control.MousePosition);
                bool bHot = false;
                if (_enableHeaderHotTrack)
                {
                    Invalidate();
                    Rectangle mouserect = new Rectangle();
                    mouserect = e.Bounds;
                    mouserect.Width += 2;
                    mouserect.Height += 2;
                    if (mouserect.Contains(mouse))
                    {
                        bHot = true;
                    }
                }
                try
                {
                    e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
                }
                catch (Exception ex)
                {
                    Console.Write(ex.Message);
                }
                Color gradStartColor = default(Color);
                Color gradEndColor = default(Color);
                Color gradMiddleColor = default(Color);
                Color borderColor = _palette.ColorTable.ToolStripBorder;
                Color textColor = _palette.ColorTable.StatusStripText;
                if (e.State == ListViewItemStates.Selected)
                {
                    gradStartColor = Color.White;
                    gradMiddleColor = _palette.ColorTable.ButtonCheckedGradientEnd;
                    gradEndColor = _palette.ColorTable.ButtonCheckedGradientBegin;
                    textColor = _palette.ColorTable.MenuItemText;
                }
                else
                {
                    if (bHot)
                    {
                        gradStartColor = Color.White;
                        gradMiddleColor = _palette.ColorTable.ButtonSelectedGradientEnd;
                        gradEndColor = _palette.ColorTable.ButtonSelectedGradientBegin;
                        textColor = _palette.ColorTable.MenuItemText;
                    }
                    else
                    {
                        gradStartColor = Color.White;
                        gradMiddleColor = _palette.ColorTable.ToolStripGradientEnd;
                        gradEndColor = _palette.ColorTable.ToolStripGradientBegin;
                        textColor = _palette.ColorTable.MenuItemText;
                    }
                }
                if (rect.Width == 0)
                {
                    rect.Width = 1;
                }
                g.FillRectangle(new SolidBrush(Color.White), rect);
                using (LinearGradientBrush brush = new LinearGradientBrush(rect, gradStartColor, gradEndColor, LinearGradientMode.Vertical))
                {
                    if (!_enableHeaderGlow)
                    {
                        DrawBlendGradient(g, rect, gradStartColor, gradEndColor, gradMiddleColor, 90f);
                    }
                    else
                    {
                        DrawListViewHeader(g, rect, gradStartColor, gradMiddleColor, gradEndColor, Color.White, 90f);
                    }
                }
                g.DrawRectangle(new Pen(borderColor), rect);
                g.DrawLine(new Pen(Color.White), new Point(rect.X + 1, rect.Y + 1), new Point(rect.X + rect.Width - 1, rect.Y + 1));
                g.DrawLine(new Pen(Color.White), new Point(rect.X + 1, rect.Y + 1), new Point(rect.X + 1, rect.Y + rect.Height - 1));
                if (e.ColumnIndex == this.Columns.Count - 1)
                {
                    g.DrawLine(new Pen(borderColor), new Point(rect.X + rect.Width - 1, rect.Y), new Point(rect.X + rect.Width - 1, rect.Y + rect.Height + 0));
                }
                if (e.State == ListViewItemStates.Selected)
                {
                    rect.Offset(3, 5);
                }
                else
                {
                    rect.Offset(2, 4);
                }
                StringFormat TextFormat = new StringFormat();
                TextFormat.FormatFlags = StringFormatFlags.NoWrap;
                g.DrawString(e.Header.Text, this.Font, new SolidBrush(textColor), rect, TextFormat);
            }
            else
            {
                e.DrawDefault = true;
            }

        }

        protected override void OnDrawItem(DrawListViewItemEventArgs e)
        {
            Rectangle rect = e.Bounds;
            Graphics g = e.Graphics;
            try
            {
                e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
            if (_palette == null)
            {
                EventArgs Ev = new EventArgs();
                OnGlobalPaletteChanged(this, Ev);
            }
            if (_persistentColors == false)
            {
                if (_useStyledColors == true)
                {
                    _gradientStartColor = Color.FromArgb(255, 246, 215);
                    _gradientEndColor = Color.FromArgb(255, 213, 77);
                    _gradientMiddleColor = Color.FromArgb(252, 224, 133);
                }
                else
                {
                    _gradientStartColor = _palette.ColorTable.StatusStripGradientBegin;
                    _gradientEndColor = _palette.ColorTable.OverflowButtonGradientEnd;
                    _gradientMiddleColor = _palette.ColorTable.StatusStripGradientEnd;
                }
            }
            Color gradStartColor = _gradientStartColor;
            Color gradEndColor = _gradientEndColor;
            Color gradMiddleColor = _gradientMiddleColor;
            Color textColor = _palette.ColorTable.StatusStripText;
            //if (_forceLeftAlign == true)
            //{
            //    foreach (ColumnHeader col in this.Columns)
            //    {
            //        col.TextAlign = HorizontalAlignment.Left;
            //    }
            //}
            ListViewItemStates sta = e.State;
            Console.Write(sta);
            if (this.View == View.Details)
            {
                if (((e.State & ListViewItemStates.Selected) != 0) && (e.Item.Selected == true))
                {
                    DrawGradient(e.Graphics, e.Bounds, gradStartColor, gradEndColor);
                    e.DrawText();
                    if (this.CheckBoxes == true)
                    {
                        string CheckState = null;
                        if (e.Item.Checked == true)
                        {
                            CheckState = "V";
                        }
                        else
                        {
                            CheckState = "O";
                        }
                        e.Graphics.DrawString(CheckState, this.Font, new SolidBrush(textColor), rect);
                        rect.Offset(19, 0);
                    }
                    if (this.SmallImageList != null && e.Item.ImageIndex >= 0 && e.Item.ImageIndex < SmallImageList.Images.Count)
                    {
                        this.SmallImageList.Draw(g, rect.X, rect.Y, 16, 16, e.Item.ImageIndex);
                    }
                }
                else
                {
                    e.DrawDefault = true;
                }
            }
            else
            {
                e.DrawDefault = true;
            }
        }

        protected override void OnDrawSubItem(DrawListViewSubItemEventArgs e)
        {
            Rectangle rect = e.Bounds;
            Graphics g = e.Graphics;
            try
            {
                e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
            Color textColor = _palette.ColorTable.StatusStripText;
            if (_persistentColors == false)
            {
                if (_useStyledColors == true)
                {
                    _gradientStartColor = Color.FromArgb(255, 246, 215);
                    _gradientEndColor = Color.FromArgb(255, 213, 77);
                    _gradientMiddleColor = Color.FromArgb(252, 224, 133);
                    textColor = _palette.ColorTable.MenuItemText;
                }
                else
                {
                    _gradientStartColor = _palette.ColorTable.StatusStripGradientBegin;
                    _gradientEndColor = _palette.ColorTable.OverflowButtonGradientEnd;
                    _gradientMiddleColor = _palette.ColorTable.StatusStripGradientEnd;
                    textColor = _palette.ColorTable.StatusStripText;
                }
            }
            Color gradStartColor = _gradientStartColor;
            Color gradEndColor = _gradientEndColor;
            Color gradMiddleColor = _gradientMiddleColor;
            //if (_forceLeftAlign == true)
            //{
            //    foreach (ColumnHeader col in this.Columns)
            //    {
            //        col.TextAlign = HorizontalAlignment.Left;
            //    }
            //}
            if (this.View == View.Details)
            {
                if ((e.ItemState & ListViewItemStates.Selected) != 0)
                {
                    DrawGradient(e.Graphics, e.Bounds, gradStartColor, gradEndColor);
                    if (e.ColumnIndex == 0)
                    {
                        try
                        {
                            //if (_indendFirstItem == true)
                            //{
                            //    rect.Offset(16, 0);
                            //}
                            this.SmallImageList.Draw(g, rect.X + 4, rect.Y, 16, 16, e.Item.ImageIndex);
                        }
                        catch (Exception ex)
                        {
                            //if (_indendFirstItem == true)
                            //{
                            //    rect.Offset(16, 0);
                            //}
                            Console.Write(ex.Message);
                        }
                    }
                    rect.Offset(4, 2);
                    e.Graphics.DrawString(e.SubItem.Text, this.Font, new SolidBrush(textColor), rect);
                }
                else
                {
                    e.DrawDefault = true;
                }
            }
            else
            {
                e.DrawDefault = true;
            }
        }

        public static void DrawGradient(Graphics g, Rectangle rect, Color DarkColor, Color LightColor)
        {
            using (LinearGradientBrush lb = new LinearGradientBrush(rect, LightColor, DarkColor, LinearGradientMode.Vertical))
            {
                Blend blend1 = new Blend(4);
                blend1.Positions[0] = 0f;
                blend1.Factors[0] = 0.9f;
                blend1.Positions[1] = 0.4f;
                blend1.Factors[1] = 0.5f;
                blend1.Positions[2] = 0.4f;
                blend1.Factors[2] = 0.2f;
                blend1.Positions[3] = 1f;
                blend1.Factors[3] = 1f;
                lb.Blend = blend1;
                g.FillRectangle(lb, rect);
            }
        }

        public static void DrawBlendGradient(Graphics g, Rectangle rect, Color LightColor, Color DarkColor, Color MiddleColor, float Angle)
        {
            ColorBlend blend = new ColorBlend(4);
            blend.Positions[0] = 0f;
            blend.Colors[0] = LightColor;
            blend.Positions[1] = 0.5f;
            blend.Colors[1] = DarkColor;
            blend.Positions[2] = 0.5f;
            blend.Colors[2] = DarkColor;
            blend.Positions[3] = 1f;
            blend.Colors[3] = MiddleColor;
            using (LinearGradientBrush b = new LinearGradientBrush(rect, blend.Colors[0], blend.Colors[3], Angle))
            {
                b.InterpolationColors = blend;
                g.FillRectangle(Brushes.White, rect);
                g.FillRectangle(b, rect);
            }
        }

        public static void DrawListViewHeader(Graphics g, Rectangle rect, Color LightColor, Color MiddleColor, Color DarkColor, Color ExtraColor, float Angle)
        {
            int HalfSize = Convert.ToInt32(rect.Height) / 2 - 2;
            Rectangle Newrect = new Rectangle(rect.X, rect.Y + HalfSize, rect.Width, rect.Height - HalfSize);
            if ((MiddleColor == Color.White) && (DarkColor == Color.White))
            {
                MiddleColor = Color.WhiteSmoke;
                DarkColor = Color.Snow;
            }
            using (LinearGradientBrush b = new LinearGradientBrush(rect, LightColor, MiddleColor, Angle))
            {
                g.FillRectangle(b, rect);
            }
            using (LinearGradientBrush b = new LinearGradientBrush(Newrect, MiddleColor, DarkColor, Angle))
            {
                g.FillRectangle(b, Newrect);
            }
        }

        private void OnGlobalPaletteChanged(object sender, EventArgs e)
        {
            if (_palette != null)
            {
                _palette.PalettePaint -= OnPalettePaint;
                //_palette.PalettePaint -= New EventHandler(Of PaletteLayoutEventArgs)(AddressOf OnPalettePaint)
            }

            _palette = KryptonManager.CurrentGlobalPalette;
            _paletteRedirect.Target = _palette;

            if (_palette != null)
            {
                _palette.PalettePaint += OnPalettePaint;
                //_palette.PalettePaint += New EventHandler(Of PaletteLayoutEventArgs)(AddressOf OnPalettePaint)
                //repaint with new values

                //set colors
                if (_persistentColors == false)
                {
                    //init color values
                    if (_useStyledColors == true)
                    {
                        _gradientStartColor = Color.FromArgb(255, 246, 215);
                        _gradientEndColor = Color.FromArgb(255, 213, 77);
                        _gradientMiddleColor = Color.FromArgb(252, 224, 133);
                    }
                    else
                    {
                        _gradientStartColor = _palette.ColorTable.StatusStripGradientBegin;
                        _gradientEndColor = _palette.ColorTable.OverflowButtonGradientEnd;
                        _gradientMiddleColor = _palette.ColorTable.StatusStripGradientEnd;
                    }
                }
            }

            Invalidate();
        }

        private void OnPalettePaint(object sender, PaletteLayoutEventArgs e)
        {
            Invalidate();
        }
    }
}
